package com.dating.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dating.activity.R;
import com.dating.entities.Her;
import com.dating.pref.Function;

@SuppressLint("ViewConstructor")
public class DtItemMap extends LinearLayout {
	Context _context;
	TextView tv_name,tv_distance;
	ImageView im_avatar,im_status;
	public Her her;
	
	boolean _status = false;
	
	LayoutInflater inflater = null;

	public DtItemMap(Context context,Her her) {
		super(context);
		init(context, false, her._fullname, her._bmp,her);
	}

	public DtItemMap(Context context, boolean status, String name,
			Bitmap avatar,Her her) {
		super(context);
		init(context, status, name, avatar,her);
	}

	public void init(Context context, boolean status, String name, Bitmap avatar,Her her) {
		this._context=context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.item_user_map, this);
		
		this.her=her;
		
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_distance=(TextView)findViewById(R.id.tv_distance);
		im_avatar = (ImageView) findViewById(R.id.im_avatar);
		im_status=(ImageView)findViewById(R.id.im_status);

		setStatus(status);
		setName(name);
		setAvatar(avatar);
		try{
		double distance=her.getDistance(Function.getLastGeopoint(_context), her.getLocation());
			tv_distance.setText(convertDistance(distance));
		}catch(NullPointerException e){
			tv_distance.setText("calculating...");
		}
	}
	public String getHerId(){
		return her._id_username;
	}
	public String convertDistance(double distance){
		double convert;
		if(distance<1500){
			return distance+" m";
		}else
			convert=distance/1000.0;
		return convert+" km";
	}
	public void setStatus(boolean status) {
		_status = status;
		if(_status){
			tv_name.setBackgroundResource(R.color.aaaa);
			tv_name.setTypeface(null,Typeface.BOLD);
			im_status.setVisibility(View.VISIBLE);
		}else{
			tv_name.setBackgroundResource(R.color.aaaa);
			tv_name.setTypeface(null,Typeface.NORMAL);
			im_status.setVisibility(View.INVISIBLE);
		}
	}

	public void setName(String name) {
		tv_name.setText(name);
	}

	public void setAvatar(Bitmap avatar) {
		if (avatar == null) {
			im_avatar.setImageResource(R.drawable.noavatar);
			return;
		}
		im_avatar.setImageBitmap(avatar);
	}
}
